home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / jcool01.zip / TEST_QUA.C < prev    next >
C/C++ Source or Header  |  1992-08-27  |  4KB  |  134 lines

  1. //
  2. // Copyright (C) 1992 General Electric Company.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // General Electric Company,
  10. // provides this software "as is" without express or implied warranty.
  11. //
  12.  
  13.  
  14. #include <cool/Quaternion.h>
  15. #include <test.h>
  16.  
  17. Boolean float_fuzz_equal (const float& f1, const float& f2) {
  18.   return ((fabs(f1 - f2) < 1.0e-7) ? TRUE : FALSE);
  19. }
  20.  
  21. void test_quaternion () {
  22.   CoolM_Vector<float> k(3, 0.0);
  23.   k.z() = 1.0;
  24.   k.set_compare(&float_fuzz_equal);
  25.  
  26.   CoolQuaternion q0;
  27.   TEST ("q0()", TRUE, TRUE);
  28.   TEST ("q0.magnitude", q0.magnitude()==1.0, TRUE);
  29.   TEST ("q0.axis", q0.axis() == k, TRUE);
  30.   TEST ("q0.angle", q0.angle() == 0.0, TRUE);
  31.   TEST ("q0.x", q0.x() == 0, TRUE);
  32.   TEST ("q0.y", q0.y() == 0, TRUE);
  33.   TEST ("q0.z", q0.z() == 0, TRUE);
  34.   TEST ("q0.r", q0.r() == 1, TRUE);
  35.   TEST ("q0==q0", q0==q0, TRUE);
  36.  
  37.   CoolQuaternion q1(k, M_PI);
  38.   TEST ("q1(axis,angle)", TRUE, TRUE);
  39.   TEST ("q1.magnitude", q1.magnitude()==1.0, TRUE);
  40.   TEST ("q1.axis", q1.axis()==k, TRUE);
  41.   TEST ("q1.angle", fabs(q1.angle()-M_PI) < 1.0e-6, TRUE);
  42.   TEST ("q1.x", q1.x() == 0, TRUE);
  43.   TEST ("q1.y", q1.y() == 0, TRUE);
  44.   TEST ("q1.z", q1.z() == 1, TRUE);
  45.   TEST ("q1.r", fabs(q1.r()) < 1.0e-6, TRUE);
  46.   TEST ("q1!=q0", q1!=q0, TRUE);
  47.   
  48.   CoolQuaternion q2((CoolM_Vector<float>&) q0);
  49.   TEST ("q2(Vector)", TRUE, TRUE);
  50.   TEST ("q0==q2", q0==q2, TRUE);
  51.   TEST ("(Vector)q", (CoolM_Vector<float>&)q0 == (CoolM_Vector<float>&)q2, TRUE);
  52.   TEST ("(Vector)q", (CoolM_Vector<float>&)q0 != (CoolM_Vector<float>&)q1, TRUE);
  53.   TEST ("q2=q1", (q2=q1, q2==q1 && q2!=q0), TRUE);
  54.  
  55.   CoolQuaternion q3(k);
  56.   TEST ("q3.axis", q3.axis()==k, TRUE);
  57.   double d = fabs(q3.angle()-M_PI);  //## Borland bug
  58.   cerr << d << endl;
  59.   TEST ("q3.angle", d < 1.0e-6, TRUE);
  60. //  TEST ("q3.angle", fabs(q3.angle()-M_PI) < 1.0e-6, TRUE);
  61. }
  62.  
  63. void test_product () {
  64.   CoolM_Vector<float> i(3,0), j(3,0), k(3,0);
  65.   i.x() = j.y() = k.z() = 1.0;
  66.   CoolQuaternion q0(k, 0), q1(i, M_PI/2), q2(j, M_PI/2), q3(k, M_PI/2);
  67.   
  68.   TEST ("1*q1==q1*1", (q0*q1)==(q1*q0), TRUE);
  69.   TEST ("(q1*q2)!=(q2*q1)", q1*q2 != q2*q1, TRUE);
  70.   TEST ("(q1*q2)*q3==q1*(q2*q3)", (q1*q2)*q3==q1*(q2*q3), TRUE);
  71.   TEST ("q1.inverse", q1.inverse() == q1.conjugate(), TRUE);
  72.   TEST ("q1*q1.inverse==q1.inverse*q1", q1*q1.inverse()==q1.inverse()*q1, TRUE);
  73.   CoolQuaternion q;
  74.   TEST ("q1*q1.conjugate==1", 
  75.     (q = q1*q1.conjugate(), q==q0), TRUE);
  76.   TEST ("(q1*q2).(q1*q3)==(q1.q1)*(q2.q3)",
  77.     fabs(dot_product((q1*q2),(q1*q3)) -
  78.          dot_product(q1,q1)*dot_product(q2,q3)) < 1.0e-6, TRUE);
  79.   TEST ("(q1*q2).(q1*q2)==(q1.q1)*(q2*q2)",
  80.     fabs(dot_product((q1*q2),(q1*q2)) -
  81.          dot_product(q1,q1)*dot_product(q2,q2)) < 1.0e-6, TRUE);
  82.   TEST ("(q1*q2).q3==q1.(q3*q2.conjugate)",
  83.     fabs(dot_product((q1*q2),q3) -
  84.          dot_product(q1,(q3*q2.conjugate()))) < 1.0e-6, TRUE);
  85. }
  86.  
  87.  
  88. void test_transform () {
  89.   CoolM_Vector<float> i(3,0.0), j(3,0.0), k(3,0.0);
  90.   i.x() = j.y() = k.z() = 1.0;
  91.   CoolQuaternion q0(k, 0), q1(i, M_PI/2), q2(j, M_PI/2), q3(k, M_PI/2);
  92.  
  93.   CoolQuaternion q(q0.rotation_transform());
  94.   TEST ("q(q0.rotation_transform)", q==q0, TRUE);
  95.   q = q1.rotation_transform();
  96.   TEST ("q=q1.rotation_transform", q==q1, TRUE);
  97.   CoolMatrix<float> rotz = q3.rotation_transform(2);
  98.   TEST ("rotz=q3.rotation_transform(2)", 
  99.     (fabs(rotz(0,0)) < 1.0e-6 && fabs(rotz(1,1)) < 1.0e-6 &&
  100.      fabs(rotz(0,1)-1) < 1.0e-6 && fabs(rotz(1,0)+1) < 1.0e-6), TRUE);
  101.   q = rotz;
  102.   TEST ("q(rotz)==q3", q==q3, TRUE);
  103.   CoolM_Vector<float> ii;
  104.   TEST ("q0.rotate(ii)", 
  105.     (ii = i, q0.rotate(ii), ii==i), TRUE);
  106.   TEST ("q1.rotate(ii)", 
  107.     (ii = i, q1.rotate(ii), ii==i), TRUE);
  108.   TEST ("q2.rotate(ii)", 
  109.     (ii = i, q2.rotate(ii), ii==-k), TRUE);
  110.   TEST ("q3.rotate(ii)", 
  111.     (ii = i, q3.rotate(ii), ii==j), TRUE);
  112. }
  113.  
  114. void test_leak () {
  115.   for (;;) {
  116.     test_quaternion();
  117.     test_product();
  118.     test_transform();
  119.   }
  120. }
  121.  
  122. int main () {
  123.   START("CoolQuaternion");
  124.   test_quaternion();
  125.   test_product();
  126.   test_transform();
  127.   
  128. #if LEAK
  129.   test_leak();
  130. #endif
  131.   SUMMARY();
  132.   return 0;
  133. }
  134.